﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Product.API.Applcation.Command.Product;
using Product.API.Dtos;
using Product.Domain.DTO;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace Product.API.Controllers
{
    /// <summary>
    /// 产品管理读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]

    public class ProductInfoController : ControllerBase
    {
        protected readonly IMediator mediator;

        public ProductInfoController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 产品添加
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProductAdd(ProductInfoCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {

                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                cd.addDTO.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.addDTO.AddName = name;
                result.Data = await mediator.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 产品修改
        /// </summary>
        /// <param name="cd"></param>
        /// <returns>ProductInfoUpdateCommand</returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProductInfoUpdate(ProductInfoUpdateCommand dto)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                dto.UpdateDTO.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                dto.UpdateDTO.UpdName = name;
                result.Data = await mediator.Send(dto);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 产品修改(出库)
        /// </summary>
        /// <param name="cd"></param>
        /// <returns>ProductInfoUpdateCommand</returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProductUpdateOutShip(ProductUpdateDTO dto)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                dto.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                dto.UpdName = name;
                result.Data = await mediator.Send(new ProductInfoUpdateCommand { UpdateDTO= dto });
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 产品删除 
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>      
        [HttpPost]
        public async Task<ApiResult<int>> ProductInfoDelete(ProductInfoDeleteCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.U_Name = name;
                result.Data = await mediator.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

    }
}
